package com.crlclr.ui
{
	import com.crl.core.IResizeable;
	import com.crl.managers.ResizeableManager;
	import com.crl.ui.controls.Component;
	import com.crlclr.managers.QTextFormat;
	import com.crlclr.utils.Sombra;
	
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	public class ErrorBar extends Component implements IResizeable
	{
		private var textField:TextField;
		
		private var timer:Timer;
		public function ErrorBar()
		{
			this.mouseEnabled=this.mouseChildren=false;
			
			_width=200;
			_height=40;
			
			timer=new Timer(2000,1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,timeCompleteHandler);
			
			super();
		}
		
		override protected function create() : void{
			textField=new TextField();
			
			textField.autoSize=TextFieldAutoSize.LEFT;
			var tf:TextFormat=QTextFormat.defaultTextFormat;
			tf.color=0;
			textField.defaultTextFormat=tf;
			
			this.addChild(textField);
			
			
			new Sombra(this);
		}
		
		override protected function draw() : void{
			var g:Graphics=this.graphics;
			
			g.beginFill(0x990000);
			
			g.drawRect(0,0,_width,_height);
			
			g.endFill();
		}
		
		
		override protected function init() : void{
			textField.y=10;
			
			this.visible=false;
			//ResizeableManager.getInstance().add(this);
		}
		
		
		public function set text(value:String):void{
			
			this.textField.text=value;
			
			this.visible=true;
			
			this.textField.x=(_width-this.textField.textWidth)>>1
			
			timer.reset();
			timer.start();
			
			ResizeableManager.getInstance().add(this);
		}
		
		private function timeCompleteHandler(event:Event):void{
			this.visible=false;
			
			ResizeableManager.getInstance().remove(this);
		}
		
		override public function resize(width:int, height:int) : void{
			this.x=(width-_width)>>1;
			this.y=(height-_height)>>1;
		}
	}
}